﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLNhaSach.BUS;
using QLNhaSach.BUS.DTO;

namespace QLNhaSach.GUI.NhanVienQuanLy
{
    public partial class XemHoaDonMotKhachHang : Form
    {
        DataTable dt = new DataTable();
        int maHD;

        public XemHoaDonMotKhachHang()
        {
            InitializeComponent();
        }

        private void ClearForm()
        {
            txtMaKH.Clear();
            txtTenKH.Clear();
            txtTenNV.Clear();
            dtpNgayLap.ResetText();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            //ClearForm();
            txtMaKH.Text = "";
            txtTenKH.Text = "";
            txtTenNV.Text = "";
            dtpNgayLap.Text = DateTime.Now.ToString();
            txtTenNV.Focus();
            XemHoaDonMotKhachHang_Load(sender, e);
        }

        private void XemHoaDonMotKhachHang_Load(object sender, EventArgs e)
        {
            dt = NhanVienKeToanBUS.DSHD();
            lvDSHD.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListViewItem item = new ListViewItem();
                item.Text = dr["MaHD"].ToString();
                item.SubItems.Add(dr["hd.MaKH"].ToString());
                item.SubItems.Add(dr["NgayCap"].ToString());             
                item.SubItems.Add(dr["TenKH"].ToString());
                lvDSHD.Items.Add(item);
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (txtTenKH.Text == "")
            {
                MessageBox.Show("Tên khách hàng không được để trống");
                txtTenKH.Focus();
                return;
            }
            //hd.MaHD, hd.NgayCap, kh.TenKH, nv.HoTen
            string sql = "SELECT * FROM ((HoaDon hd INNER JOIN ";
            sql += "KhachHang kh ON hd.MaKH = kh.MaKH) INNER JOIN NhanVien nv ON hd.MaNV = nv.MaNV) WHERE ";
            sql += " (kh.TenKH LIKE '%" + txtTenKH.Text + "%') ";
            if (txtTenNV.Text != "")
                sql += " OR (nv.HoTen LIKE '%" + txtTenNV.Text + "%') ";
            sql += " OR (hd.NgayCap = #" + dtpNgayLap.Text + "#) ";
            if (txtMaKH.Text != "")
                sql += " OR (kh.MaKH = " + Convert.ToInt16(txtMaKH.Text) + ")";
            dt = NhanVienKeToanBUS.TimHoaDon(sql);
            lvDSHD.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListViewItem item = new ListViewItem();
                item.Text = dr["MaHD"].ToString();
                item.SubItems.Add(dr["hd.MaKH"].ToString());
                item.SubItems.Add(dr["NgayLap"].ToString());
                item.SubItems.Add(dr["HoTen"].ToString());
                item.SubItems.Add(dr["TenKH"].ToString());
                lvDSHD.Items.Add(item);
            }
        }

        private void btnCTHD_Click(object sender, EventArgs e)
        {
            DataTable dthd = new DataTable();
            string sql = "SELECT DISTINCT s.TenSach, cthd.SoLuong, cthd.DonGia ";
            sql += "FROM (((CTHoaDon cthd INNER JOIN HoaDon hd ON cthd.MaHD = hd.MaHD) INNER JOIN ";
            sql += "KhachHang kh ON hd.MaKH = kh.MaKH) INNER JOIN Sach s ON cthd.MaSach = s.MaSach) ";
            sql += " WHERE (cthd.MaHD = " + maHD + ")";
            dthd = NhanVienKeToanBUS.TimHoaDon(sql);
            lvDSCTHD.Items.Clear();
            for (int i = 0; i < dthd.Rows.Count; i++)
            {
                DataRow dr = dthd.Rows[i];
                ListViewItem item = new ListViewItem();
                item.Text = dr["TenSach"].ToString();
                item.SubItems.Add(dr["SoLuong"].ToString());
                item.SubItems.Add(dr["DonGia"].ToString());
                double sl = Convert.ToDouble(dr["SoLuong"]);
                double dg = Convert.ToDouble(dr["DonGia"]);
                double tt = sl * dg;
                item.SubItems.Add(tt.ToString());
                lvDSCTHD.Items.Add(item);
            }
        }

        

        private void lvDSHD_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = lvDSHD.FocusedItem.Index;
            DataRow dr = dt.Rows[index];
            maHD = Convert.ToInt16(dr["MaHD"]);
        }
       
    }
}